package org.ladybug.service.auth;

import java.util.ArrayList;
import java.util.Collection;

import org.ladybug.dao.persistence.Ruolo;
import org.ladybug.dao.persistence.Utente;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

public class LadyBugUserDetails implements UserDetails {

	private Utente utente = null;
	private Collection<GrantedAuthority> collAuthorities=new ArrayList<GrantedAuthority>();
	
	public LadyBugUserDetails() {
	}
	
	public void addAuthority(GrantedAuthority authority){
		collAuthorities.add(authority);
	}

	public GrantedAuthority[] getAuthorities() {
		GrantedAuthority[] authorities = new LadybugGrantAuthority[collAuthorities.size()];
		collAuthorities.toArray(authorities);
		return authorities;
	}

	public String getPassword() {
		String password = utente.getPassword();
		return password;
	}

	public String getUsername() {
		// TODO Auto-generated method stub
		return utente.getNickname();
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}

	
	//--------------------------------------- get e set properties
	public Utente getUtente() {
		return utente;
	}

	public void setUtente(Utente utente) {
		this.utente = utente;
	}


	//---------------------------------------- INNER CLASSES
	
	public class LadybugGrantAuthority implements GrantedAuthority{
		
		private Ruolo ruolo;

		public LadybugGrantAuthority(Ruolo ruolo){
			this.ruolo=ruolo;
		}
		public String getAuthority() {
			
			return this.ruolo.getDescrizione();
			
		}

		public int compareTo(Object o) {
			if (o instanceof LadybugGrantAuthority) {
				LadybugGrantAuthority theOther = (LadybugGrantAuthority) o;
				return getAuthority().compareTo(theOther.getAuthority());
			}
			return 0;
		}
		
	}

}
